package frsf.cidisi.exercise.tpidemia.search.actions;

import java.util.ArrayList;

import frsf.cidisi.exercise.tpidemia.search.*;
import frsf.cidisi.faia.agent.search.SearchAction;
import frsf.cidisi.faia.agent.search.SearchBasedAgentState;
import frsf.cidisi.faia.state.AgentState;
import frsf.cidisi.faia.state.EnvironmentState;

public class ViajarABresol extends SearchAction {

    /**
     * This method updates a tree node state when the search process is running.
     * It does not updates the real world state.
     */
    @Override
    public SearchBasedAgentState execute(SearchBasedAgentState s) {
        ArqueologoState agState = (ArqueologoState) s;
        String destino = "Bresol";
        ArrayList<String> adyacentes = new ArrayList<String>();
        adyacentes.addAll(agState.getAdyacentes(agState.getPosicion()));
        if(adyacentes.contains(destino)  && !(agState.getVisitadas2().contains(destino))){
        	double distanciaV = agState.getDistanciaRecorrida();
        	double distanciaN = agState.calcularDistancia(agState.getPosicion(), destino);
        	
        	agState.setDistanciaRecorrida(distanciaV + distanciaN);
        	agState.setposicion(destino);
        	if(agState.getVisitadas1().contains(destino))
        	{
        		agState.getVisitadas2().add(destino);
        	}
        	else{
        		agState.getVisitadas1().add(destino);
        	}
        	return agState;
        	//Actualizar distancia
        	
        }
        
        return null;
    }

    /**
     * This method updates the agent state and the real world state.
     */
    @Override
    public EnvironmentState execute(AgentState ast, EnvironmentState est) {
        ArqueologoEstadoEntorno environmentState = (ArqueologoEstadoEntorno) est;
        ArqueologoState agState = ((ArqueologoState) ast);

        String destino = "Bresol";
        ArrayList<String> adyacentes = new ArrayList<String>();
        adyacentes.addAll(environmentState.getAdyacentes(agState.getPosicion()));
        if(adyacentes.contains(destino) && !(agState.getVisitadas2().contains(destino))){
        	double distanciaN = agState.calcularDistancia(agState.getPosicion(), destino);
        	double distanciaV = agState.getDistanciaRecorrida();
        	
        	agState.setDistanciaRecorrida(distanciaV + distanciaN);
        	environmentState.setarqueologoPosicion(destino);
        	agState.setposicion(destino);
        	if(agState.getVisitadas1().contains(destino))
        	{
        		agState.getVisitadas2().add(destino);
        	}
        	else{
        		agState.getVisitadas1().add(destino);
        	}
        	        	//Actualizar distancia
        
        }
        return environmentState;

    
    }
    

    /**
     * This method returns the action cost.
     */
    @Override
    public Double getCost() {
        return new Double(0);
    }

    /**
     * This method is not important for a search based agent, but is essensial
     * when creating a calculus based one.
     */
    @Override
    public String toString() {
        return "Viajar a BRESOL";
    }
}